// 本地 临时存储
function getSdata(k, isJson) {
    if (!sessionStorage[k]) return false;
    if (sessionStorage[k] == "undefined") return {};
    var k;
    if (!isJson) {
        try {
            k = JSON.parse(sessionStorage[k])
        } catch (e) {
            k = sessionStorage[k]
        }
    } else {
        k = sessionStorage[k]
    }
    return k
}
function setSdata(k, v){
    if(typeof v != 'string'){
        v = JSON.stringify(v)
    }
    sessionStorage[k] = v;
}
function delSdata(k) {
    delete sessionStorage[k];
}
/**
 * 功能:设置得变量name的值,存储到cookie中
 * @param {[string]} name  [参数名]
 * @param {[string]} value [参数值]
 * @param {[string]} url   [接收的页面]
 * @param {[string]} hour   [保存的小时数]
 * 实例:setCookie('username','baobao')
 */
function setCookie(name,value,hour) {
    if(value == '{}')return
    hour = hour >= 0 ? hour : 10000;
    value = typeof value === 'string'?  value : JSON.stringify(value);
    var exp  = new Date();
    exp.setTime(exp.getTime() + (hour*60*60*1000));
    /*if(debug && location.host != 'api.juewei.com'){
     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;";
     }else {
     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;domain=juewei.com";
     }*/
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;domain=xhs1985.com";
}
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        try{
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
            document.cookie= name + "="+cval+";expires="+exp.toGMTString()+ ";path=/;domain=xhs1985.com";
        } catch(e) {
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }

}
/**
 * 功能:取得cookie中 变量name的值
 * @param  {[string]} name [参数名]
 * @return {[string]}      [返回参数值]
 * 实例:alert(getCookie("baobao"));
 */
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(name === 'commonable') {try{return JSON.parse(unescape(arr[2]));}catch(e) {console.log('获取失败')}};
    if(arr !=null) {try{return JSON.parse(unescape(arr[2]));}catch(e) {console.log('json 获取失败'); return unescape(arr[2]); }}; return null;
}
// 末尾金额去零 如果带小数点 就去小数点
var moneyReZero = function(m){
    if(typeof m == "number") { m = m.toFixed(2);}
    if(!/\./g.test(m))return m
    var money = m.replace(/0$/g,"")
    if(/0$/g.test(money)){
        money =  moneyReZero(money)
    }
    money = money.replace(/\.$/,"");
    return money || 0;
}
var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;
function getRad(d){
    return d*PI/180.0;
}
/**
 * approx distance between two points on earth ellipsoid
 * @param {Object} lat1
 * @param {Object} lng1
 * @param {Object} lat2
 * @param {Object} lng2
 */
function getFlatternDistance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2);
    var g = getRad((lat1 - lat2)/2);
    var l = getRad((lng1 - lng2)/2);

    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);

    var s,c,w,r,d,h1,h2;
    var a = EARTH_RADIUS;
    var fl = 1/298.257;

    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;

    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;

    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;

    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}
$.extend({
   toast: function(msg,time){
       var $toast = $('<div class="modal toast">'+msg+'</div>').appendTo(document.body);
       $toast.show();
       $toast.css({
           marginTop: - Math.round($toast.outerHeight() / 2) + 'px',
           marginLeft: - Math.round(parseInt(window.getComputedStyle($toast[0]).width) / 2)  + 'px'
       });
       $toast.addClass('modal-in');
       setTimeout(function () {
           $toast.removeClass('modal-in').addClass('modal-out').remove();
       },time || 2000);
   },
   alert: function (title,callback) {
        var $dialog = $('<div data-role="widget" data-widget="sku_dialog" class="sku_dialog" id="showDiv" style=" display:block;">' +
                            '<div class="widget_mask"></div>' +
                            '<div class="popup">' +
                            '<div class="popup_con"><p>'+title+'</p></div>' +
                                '<div class="popup_bot"><a>确定</a></div>' +
                            '</div>' +
                        '</div>').appendTo(document.body);
        $dialog.show().find('.popup_bot').bind('click',function () {
            if(callback) callback();
            $dialog.hide().remove();
        });
    }
});

